package com.example.controller;


import com.example.pojo.Disciple;
import com.example.pojo.Result;
import com.example.service.DiscipleService;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private DiscipleService discipleService;

    @PostMapping("/login")
    public Result login(@RequestBody Disciple disciple) {
        log.info("员工登录：{}", disciple);
        Disciple e = discipleService.login(disciple);
        //登录成功，生成令牌，下发令牌
        if (e != null) {
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("name", e.getName());
            claims.put("username", e.getUsername());
            String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }

        //登录失败，返回错误信息

        return Result.error("用户名或密码错误");
    }


}
